<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title>Ray collision test</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="css/webgl/style.css">
	</head>
	
	<body>

		<div id="infobutton">
			<a href="javascript:toggleInfo()"><img src="images/webgl/i.png" border="0"></a>
		</div>
		<div id="info">
				<B>Ray collision test - WebGL(html5)</B><BR>
				Arrowkeys to move. Space to jump.
				<P>A test to do some sort of collision checking. By shooting a ray and see if it hits a mesh.<BR>It's probably a stupid/ineffective way of doing this, don't know how these things are<BR>usually dealt with when it comes to 3d.</P>
				Done using <a href="https://github.com/mrdoob/three.js" target="_blank">three.js</a>.
				<P><B>Note.</B> You need a modern browser that supports WebGL for this to run the way it is intended.<BR>
				For example. <a href="http://www.google.com/landing/chrome/beta/" target="_blank">Google Chrome 9+</a> or <a href="http://www.mozilla.com/firefox/beta/" target="_blank">Firefox 4+</a>.</P>
				<font color="#777777">(C) OutsideOfSociety 2010.
		</div>

		<script type="text/javascript" src="js/threejs/ThreeE.js"></script>

		<script type="text/javascript" src="js/room/keys.js"></script>

		<script type="text/javascript" src="js/threejs/RequestAnimationFrame.js"></script>
		<script type="text/javascript" src="js/threejs/Stats.js"></script>
		<script type="text/javascript" src="js/room/info.js"></script>


		<script type="text/javascript">

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;
			var FLOOR = -250;

			var container;
			var stats;

			var camera;
			var scene;
			var webglRenderer;

			var mesh, zmesh, lightMesh, geometry;
			
			var directionalLight, pointLight;
			
			var mouseX = 0;
			var mouseY = 0;

			var windowHalfX = window.innerWidth >> 1;
			var windowHalfY = window.innerHeight >> 1;

			var render_gl = 1;
			var has_gl = 0;
			
			var cubeRefMesh;
			var collideMesh;

			var r = 0;
			
			var gravity = 1;
			var yspeed = 0;
			var onGround = true;


      var mouse2D= new THREE.Vector3( 0, 0, 0);
			init(), animate();
				
			
				
			

      function addMesh( geometry, scale, x, y, z, rx, ry, rz, material,name ) {
            geometry.computeBoundingBox();
            
            
    
        mesh = new THREE.Mesh( geometry, material );
        mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
        mesh.position.x = x;
        mesh.position.y = y;
        mesh.position.z = z;
        mesh.rotation.x = rx;
        mesh.rotation.y = ry;
        mesh.rotation.z = rz;
        mesh.overdraw = true;
        mesh.doubleSided = false;
        mesh.name=name;
        mesh.size=geometry.boundingBox.max.subSelf(geometry.boundingBox.min);
        mesh.updateMatrix();


        scene.add(mesh);
        
        return mesh;
      
      }
			
			function init() {

				container = document.createElement('div');
				document.body.appendChild(container);
				
				var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;

				camera = new THREE.Camera( 75, aspect, 1, 100000 );
				camera.position.z = 600;
				camera.position.x = 0;
				camera.position.y = 200;

				camera.updateMatrix();

				scene = new THREE.Scene();

				// LIGHTS

				var ambient = new THREE.AmbientLight( 0x101010 );
				scene.add( ambient );

				directionalLight = new THREE.DirectionalLight( 0xffffff, 1.0 );
				directionalLight.position.x = 1;
				directionalLight.position.y = 1;
				directionalLight.position.z = 2;
				directionalLight.position.normalize();
				scene.add( directionalLight );

				pointLight = new THREE.PointLight( 0x666666 );
				pointLight.position.x = 0;
				pointLight.position.y = 0;
				pointLight.position.z = 0;
				scene.add( pointLight );
				
				var cubeRef = new THREE.CubeGeometry( 100, 100, 100, 1, 1 );

				cubeRefMesh = addMesh( cubeRef, 1,  -300, FLOOR+50, 0, 0,0,0, new THREE.MeshLambertMaterial( { color: 0xFF0000 } ),'ref' );
				camera.target = cubeRefMesh;

				var collideCube = new THREE.CubeGeometry( 20, 20, 20, 1, 1 );
				// collideMesh = addMesh( collideCube, 1,  -300, FLOOR+5000, 0, 0,0,0, new THREE.MeshLambertMaterial( { color: 0xC0C0C0 } ) );

				
				
				var cube1 = new THREE.CubeGeometry( 10, 300, 300, 1, 1 );				
				addMesh( cube1, 1,  -200, FLOOR+150, 0, 0,0,0, getMaterial(),'x10' );
				
        var cube2 = new THREE.CubeGeometry( 10, 300, 300, 1, 1 );       
        addMesh( cube2, 1,  -150, FLOOR+50, 0, 0,0,0, getMaterial(),'x10-2' );
				
				var cube2 = new THREE.CubeGeometry( 300, 10, 300, 1, 1 );       
        addMesh( cube2, 1,  10, FLOOR+20, 0, 0,0,0, getMaterial(),'y10' );
        
        var cube3 = new THREE.CubeGeometry( 300, 300, 10, 1, 1 );       
        addMesh( cube3, 1,  500, FLOOR+150, 0, 0,0,0, getMaterial(),'z10' );
        
				
				var cube = new THREE.CubeGeometry( 300, 300, 300, 1, 1 );
				addMesh( cube, 1,  900, FLOOR+150, 400, 0,0,0, getMaterial(),'bottom' );
				addMesh( cube, 1,  900, FLOOR+150+300+20, 400, 0,0,0, getMaterial(),'top' );
        addMesh( cube, 1,  900+350, FLOOR+150, 400, 0,0,0, getMaterial(),'left' );
        
				addMesh( cube, 1,  0, FLOOR+150, -1500, 0,1.57,0, getMaterial() );
				addMesh( cube, 1,  -600, FLOOR+150, -1500, 0,1.57,0, getMaterial() );
				addMesh( cube, 1,  -1200, FLOOR+150+300, -1500, 0,1.57,0, getMaterial(true) );

				addMesh( cube, 1,  0, FLOOR+150, -2100, 0,1.57,0, getMaterial() );

				plane = new THREE.PlaneGeometry( 100, 100, 15, 10 );
				
				for(var i=0; i<plane.faceUvs.length; i++) {
					var uvs = plane.faceUvs[i];
					for ( j = 0, jl = uvs.length; j < jl; j++ ) {
						uvs[j].u *= 10;
						uvs[j].v *= 10;
					}
					
				}

				addMesh( plane, 100,  0, FLOOR, 0, -1.57,0,0, getFloorMaterial(),'plane' );

				try {
					webglRenderer = new THREE.WebGLRenderer( scene );
					webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
					container.appendChild( webglRenderer.domElement );
					has_gl = 1;
				}
				catch (e) {
					// need webgl
					document.getElementById('info').innerHTML = "<P><BR><B>Note.</B> You need a modern browser that supports WebGL for this to run the way it is intended.<BR>For example. <a href='http://www.google.com/landing/chrome/beta/' target='_blank'>Google Chrome 9+</a> or <a href='http://www.mozilla.com/firefox/beta/' target='_blank'>Firefox 4+</a>.</P><CENTER><BR><img src='../general/WebGL_logo.png' border='0'></CENTER>";
					document.getElementById('info').style.display = "block";
					return;
				}


				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild( stats.domElement );
				
				document.addEventListener( 'keydown', keyD, false );
				document.addEventListener( 'keyup', keyU, false );
				document.addEventListener( 'mousemove', function(event){click(event);}, false );
			}
			
			function click(event){
        event.preventDefault();
        mouse2D.x = ( event.clientX / SCREEN_WIDTH ) * 2 - 1;
        mouse2D.y = - ( event.clientY / SCREEN_HEIGHT  ) * 2 + 1;
        setTimeout("locate(mouse2D)",0);
			}
			
			function locate(mouse2D){
        projector = new THREE.Projector();
        ray = projector.pickingRay(mouse2D.clone(),camera );
        var intersects = ray.intersectScene( scene );
        intersector = getRealIntersector( intersects );
        // cubeRefMesh.position.copy(intersector.point); 
        
          if ( intersector ) {
                    
                              var ctrPosition=cubeRefMesh.position;
                              var ctrSize=cubeRefMesh.size;
                              var tchPoint=intersector.point;
                              var tchPosition=intersector.object.position;
                              var targetPoint=new THREE.Vector3();
                              // targetPoint.copy(cubeRefMesh.position);
                              var interSize=intersector.object.size;
                              var tmpPoint=new THREE.Vector3;
                              tmpPoint.copy(tchPoint);
                              if(interSize.x/2-Math.abs(tchPoint.x-tchPosition.x)<.15){
                                  // tchPoint.x=Math.floor( tchPoint.x / interSize.x ) * interSize.x + interSize.x/2;
                                  tmpPoint.x=tchPoint.x;
                                  if(tchPoint.x>tchPosition.x){
                                    
                                    //use chceckIntersctX
                                    if(!checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh)){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x+ctrSize.x/2;
                                        cubeRefMesh.position.copy(targetPoint);
                                    }
                                    
                                       
                                  }
                                  else{
                                    if(!checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh)){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x-ctrSize.x/2;
                                        cubeRefMesh.position.copy(targetPoint);
                                    }
                                  }

                              }
                                    
                              if(interSize.y/2-Math.abs(tchPoint.y-tchPosition.y)<.15){
                                  // tchPoint.y=Math.floor( tchPoint.y / interSize.y ) * interSize.y + interSize.y/2;
                                  tmpPoint.y=tchPoint.y;
                                  if(tchPoint.y>tchPosition.y){
                                    
                                    //use chceckIntersctZ
                                    if(!checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh)){
                                        targetPoint.y=tchPoint.y+ctrSize.y/2;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x;
                                        cubeRefMesh.position.copy(targetPoint);
                                    }
                                    
                                       
                                  }
                                  else{
                                    if(!checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh)){
                                        targetPoint.y=tchPoint.y-ctrSize.y/2;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x;
                                        cubeRefMesh.position.copy(targetPoint);
                                    }
                                  }

                              } 
                              if(interSize.z/2-Math.abs(tchPoint.z-tchPosition.z)<.15){
                                  // tchPoint.z=Math.floor( tchPoint.z / interSize.z ) * interSize.z + interSize.z/2;
                                  tmpPoint.z=tchPoint.z;
                                  if(tchPoint.z>tchPosition.z){
                                    
                                    //use chceckIntersctZ
                                    if(!checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh)){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z+ctrSize.z/2;
                                        targetPoint.x=tchPoint.x;
                                        cubeRefMesh.position.copy(targetPoint);
                                    }
                                    
                                       
                                  }
                                  else{
                                    if(!checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh)){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z+ctrSize.z/2;
                                        targetPoint.x=tchPoint.x;
                                        cubeRefMesh.position.copy(targetPoint);
                                    }
                                  }

                              }                              
                              
                         }       

			}
			function checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh){
			// I.x.Max=Min(C.Max.x,S.Max.x)	I.x.Min=Max(C.Min.x,S.Min.x)												
			var ctrMax=new THREE.Vector3(ctrSize.x/2+tmpPoint.x,ctrSize.y/2+tmpPoint.y,ctrSize.z/2+tmpPoint.z);
          	var ctrMin=new THREE.Vector3(tmpPoint.x-ctrSize.x/2,tmpPoint.y-ctrSize.y/2,tmpPoint.z-ctrSize.z/2);
          	for(var i in scene.children){
          		var g=scene.children[i];
				if( g instanceof THREE.Mesh && g!=intersector.object && g.name!='ref' && g.name!='plane'){
              	var gMax=new THREE.Vector3(g.position.x+g.size.x/2,g.position.y+g.size.y/2,g.position.z+g.size.z/2);
              	var gMin=new THREE.Vector3(g.position.x-g.size.x/2,g.position.y-g.size.y/2,g.position.z-g.size.z/2);
              	if(Math.min(ctrMax.x,gMax.x)>Math.max(ctrMin.x,gMin.x)&&Math.min(ctrMax.y,gMax.y)>Math.max(ctrMin.y,gMin.y)&&Math.min(ctrMax.z,gMax.z)>Math.max(ctrMin.z,gMin.z)) 

              	return true;
          	}}
			
				return false;
			}
			// function checkIntersect(tmpPoint,ctrSize,intersector,cubeRefMesh){
          // // s'MaxX=s'.size.x/2+s'.position.x/2;   
          // // s'MinX=s'.position.x-s'.size.x/2;   
          // // for ( a as SettingGoodsInRoom){   
          // //   g=a.Goods;
          // //   gMaxX=g.size.x/2+g.position.x;
          // //   gMinX=g.position.x-g.size.x/2;
          // //   if(s'MaxX>gMaxX &&s'MinX<gMaxX)||(s'MaxX>gMinX &&s'MinX<gMinX)
          // // 
          // // }
//           
          // var ctrMax=new THREE.Vector3(ctrSize.x/2+tmpPoint.x,ctrSize.y/2+tmpPoint.y,ctrSize.z/2+tmpPoint.z);
          // var ctrMin=new THREE.Vector3(tmpPoint.x-ctrSize.x/2,tmpPoint.y-ctrSize.y/2,tmpPoint.z-ctrSize.z/2);
//           
          // for(var i in scene.children){
              // g=scene.children[i];
//               
              // if( g instanceof THREE.Mesh && g!=intersector.object && g!=cubeRefMesh && g.name!='plane'){
              // var gMax=new THREE.Vector3(g.position.x+g.size.x/2,g.position.y+g.size.y/2,g.position.z+g.size.z/2);
              // var gMin=new THREE.Vector3(g.position.x-g.size.x/2,g.position.y-g.size.y/2,g.position.z-g.size.z/2);
              // var bIntersect=((ctrMax.x>gMax.x &&ctrMin.x<gMax.x)||(ctrMax.x>gMin.x&& ctrMin.x<gMin.x)) &&
                 // ((ctrMax.y>gMax.y &&ctrMin.y<gMax.y)||(ctrMax.y>gMin.y&& ctrMin.y<gMin.y)) &&
                 // ((ctrMax.z>gMax.z &&ctrMin.z<gMax.z)||(ctrMax.z>gMin.z&& ctrMin.z<gMin.z));
              // if(bIntersect){
                   // return true;
                 // }
               // }
          // }
          // return false;
//                   
			// }
			
			function getRealIntersector(intersects){
        if ( intersects.length > 0 ) {
          for(i in intersects){
            
            console.warn(intersects[i].object.name);
        
          }
        }
        return intersects[0];
			}
			
			
      function getMaterial (qmark) {
          var texturePainting =null;
          if(qmark){
             texturePainting = THREE.ImageUtils.loadTexture( "images/webgl/qmark1.png", THREE.UVMapping);  
          }else{
            texturePainting = THREE.ImageUtils.loadTexture( "images/webgl/rock.png", THREE.UVMapping); 
          }
          imageTextureMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting } );
          return imageTextureMaterial;
          
      }


      function getFloorMaterial () {
        
            var texturePainting =null;
            texturePainting = THREE.ImageUtils.loadTexture( "images/webgl/grid.png", THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping); 
            floorTextureMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting } );
            return floorTextureMaterial;
      }

			function control() {
				var speed = 10;
				var halfSize = 50;
				var nearHalfSize = halfSize-5;

				if (keyUp) {
					cubeRefMesh.position.z -= speed;
				}
				if (keyDown) {
					cubeRefMesh.position.z += speed;
				}
				if (keyLeft) {
					cubeRefMesh.position.x -= speed;
				}
				if (keyRight) {
					cubeRefMesh.position.x += speed;
				}
				if (keySpace && onGround) {
					yspeed = 30;
					onGround = false;
				}
			
				collideMesh.position.y = 6000;
				
				onGround = false;
				// y
				yspeed -= gravity;
				cubeRefMesh.position.y += yspeed;
				
				var down_vector = new THREE.Vector3( 0, -1, 0 );
				var up_vector = new THREE.Vector3( 0, 1, 0 );

				var bl = new THREE.Vector3( cubeRefMesh.position.x-halfSize, cubeRefMesh.position.y, cubeRefMesh.position.z-halfSize );
				var br = new THREE.Vector3( cubeRefMesh.position.x+halfSize, cubeRefMesh.position.y, cubeRefMesh.position.z-halfSize );
				var fl = new THREE.Vector3( cubeRefMesh.position.x-halfSize, cubeRefMesh.position.y, cubeRefMesh.position.z+halfSize );
				var fr = new THREE.Vector3( cubeRefMesh.position.x+halfSize, cubeRefMesh.position.y, cubeRefMesh.position.z+halfSize );
				
				// down
				if (yspeed < 0) {
					// bl
					var bl_ray = new THREE.Ray( bl, down_vector );
					var bl_intersects = bl_ray.intersectScene( scene );
					if ( bl_intersects.length > 0 && bl_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = bl_intersects[0].point.y+halfSize;
						onGround = true;
					}
					// br
					var br_ray = new THREE.Ray( br, down_vector );
					var br_intersects = br_ray.intersectScene( scene );
					if ( br_intersects.length > 0 && br_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = br_intersects[0].point.y+halfSize;
						onGround = true;
					}
					// fl
					var fl_ray = new THREE.Ray( fl, down_vector );
					var fl_intersects = fl_ray.intersectScene( scene );
					if ( fl_intersects.length > 0 && fl_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = fl_intersects[0].point.y+halfSize;
						onGround = true;
					}	
					// fr
					var fr_ray = new THREE.Ray( fr, down_vector );
					var fr_intersects = fr_ray.intersectScene( scene );
					if ( fr_intersects.length > 0 && fr_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = fr_intersects[0].point.y+halfSize;
						onGround = true;
					}	
				}
				// up
				if (yspeed > 0) {
					// bl
					var bl_ray = new THREE.Ray( bl, up_vector );
					var bl_intersects = bl_ray.intersectScene( scene );
					if ( bl_intersects.length > 0 && bl_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = bl_intersects[0].point.y-halfSize;
						yspeed = 0;
					}
					// br
					var br_ray = new THREE.Ray( br, up_vector );
					var br_intersects = br_ray.intersectScene( scene );
					if ( br_intersects.length > 0 && br_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = br_intersects[0].point.y-halfSize;
						yspeed = 0;
					}
					// fl
					var fl_ray = new THREE.Ray( fl, up_vector );
					var fl_intersects = fl_ray.intersectScene( scene );
					if ( fl_intersects.length > 0 && fl_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = fl_intersects[0].point.y-halfSize;
						yspeed = 0;
					}	
					// fr
					var fr_ray = new THREE.Ray( fr, up_vector );
					var fr_intersects = fr_ray.intersectScene( scene );
					if ( fr_intersects.length > 0 && fr_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.y = fr_intersects[0].point.y-halfSize;
						yspeed = 0;
					}	
				}
				
				if (onGround) {
					yspeed = 0;
				}

				cubeRefMesh.updateMatrix();

				// ray
				var f_vector = new THREE.Vector3( 0, 0, -1 );
				var b_vector = new THREE.Vector3( 0, 0, 1 );
				var l_vector = new THREE.Vector3( -1, 0, 0 );
				var r_vector = new THREE.Vector3( 1, 0, 0 );

				var left = new THREE.Vector3( cubeRefMesh.position.x-halfSize, cubeRefMesh.position.y-nearHalfSize, cubeRefMesh.position.z );
				var right = new THREE.Vector3( cubeRefMesh.position.x+halfSize, cubeRefMesh.position.y-nearHalfSize, cubeRefMesh.position.z );
				var front = new THREE.Vector3( cubeRefMesh.position.x, cubeRefMesh.position.y-nearHalfSize, cubeRefMesh.position.z+halfSize );
				var back = new THREE.Vector3( cubeRefMesh.position.x, cubeRefMesh.position.y-nearHalfSize, cubeRefMesh.position.z-halfSize );
				
				
               //          
               //   * ********start added by Ben on 2012/2/12 04:26********
               //   */   
               //  var hleft = new THREE.Vector3( cubeRefMesh.position.x-halfSize, cubeRefMesh.position.y+nearHalfSize, cubeRefMesh.position.z );
               //  var hright = new THREE.Vector3( cubeRefMesh.position.x+halfSize, cubeRefMesh.position.y+nearHalfSize, cubeRefMesh.position.z );
               //  var hfront = new THREE.Vector3( cubeRefMesh.position.x, cubeRefMesh.position.y+nearHalfSize, cubeRefMesh.position.z+halfSize );
               //  var hback = new THREE.Vector3( cubeRefMesh.position.x, cubeRefMesh.position.y+nearHalfSize, cubeRefMesh.position.z-halfSize );
               // // front
               //  // if (keyUp) {
               //    var left_ray = new THREE.Ray( hleft, f_vector );
               //    var left_intersects = left_ray.intersectScene( scene );
               //    if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.z = left_intersects[0].point.z+halfSize+1;
               //      console.warn("lf==="+left_intersects[0].object.name);
               //    }
               //    var right_ray = new THREE.Ray( hright, f_vector );
               //    var right_intersects = right_ray.intersectScene( scene );
               //    if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.z = right_intersects[0].point.z+halfSize+1;
               //      console.warn("rf==="+right_intersects[0].object.name);
               //    }
               //  // }
               //  // back
               //  // if (keyDown) {
               //    var left_ray = new THREE.Ray( hleft, b_vector );
               //    var left_intersects = left_ray.intersectScene( scene );
               //    if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.z = left_intersects[0].point.z-halfSize-1;
               //      console.warn("lb==="+left_intersects[0].object.name);
               //    }
               //    var right_ray = new THREE.Ray( hright, b_vector );
               //    var right_intersects = right_ray.intersectScene( scene );
               //    if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.z = right_intersects[0].point.z-halfSize-1;
               //      console.warn("rb==="+right_intersects[0].object.name);
               //    }
               //  // }        
               //  // right
               //  // if (keyRight) {
               //    var back_ray = new THREE.Ray( hback, r_vector );
               //    var back_intersects = back_ray.intersectScene( scene );
               //    if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.x = back_intersects[0].point.x-halfSize-1;
               //      console.warn("br==="+back_intersects[0].object.name);
               //    }
               //    var front_ray = new THREE.Ray( hfront, r_vector );
               //    var front_intersects = front_ray.intersectScene( scene );
               //    if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.x = front_intersects[0].point.x-halfSize-1;
               //      console.warn("fr==="+front_intersects[0].object.name);
               //    }
               //  // }
               //  // left
               //  // if (keyLeft) {
               //    var back_ray = new THREE.Ray( hback, l_vector );
               //    var back_intersects = back_ray.intersectScene( scene );
               //    if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.x = back_intersects[0].point.x+halfSize+1;
               //      console.warn("bl==="+back_intersects[0].object.name);
               //    }
               //    var front_ray = new THREE.Ray( hfront, l_vector );
               //    var front_intersects = front_ray.intersectScene( scene );
               //    if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
               //      cubeRefMesh.position.x = front_intersects[0].point.x+halfSize+1;
               //      console.warn("fl==="+front_intersects[0].object.name);
               //    }
               //  // }
               //  
               //  /*
               //   * ********end added by Ben********
               //          
               //         
          
        
				// front
				// if (keyUp) {
					var left_ray = new THREE.Ray( left, f_vector );
					var left_intersects = left_ray.intersectScene( scene );
					if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.z = left_intersects[0].point.z+halfSize+1;
						console.warn("lf==="+left_intersects[0].object.name);
					}
					var right_ray = new THREE.Ray( right, f_vector );
					var right_intersects = right_ray.intersectScene( scene );
					if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.z = right_intersects[0].point.z+halfSize+1;
						console.warn("rf==="+right_intersects[0].object.name);
					}
				// }
				// back
				// if (keyDown) {
					var left_ray = new THREE.Ray( left, b_vector );
					var left_intersects = left_ray.intersectScene( scene );
					if ( left_intersects.length > 0 && left_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.z = left_intersects[0].point.z-halfSize-1;
						console.warn("lb==="+left_intersects[0].object.name);
					}
					var right_ray = new THREE.Ray( right, b_vector );
					var right_intersects = right_ray.intersectScene( scene );
					if ( right_intersects.length > 0 && right_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.z = right_intersects[0].point.z-halfSize-1;
						console.warn("rb==="+right_intersects[0].object.name);
					}
				// }				
				// right
				// if (keyRight) {
					var back_ray = new THREE.Ray( back, r_vector );
					var back_intersects = back_ray.intersectScene( scene );
					if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.x = back_intersects[0].point.x-halfSize-1;
						console.warn("br==="+back_intersects[0].object.name);
					}
					var front_ray = new THREE.Ray( front, r_vector );
					var front_intersects = front_ray.intersectScene( scene );
					if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.x = front_intersects[0].point.x-halfSize-1;
						console.warn("fr==="+front_intersects[0].object.name);
					}
				// }
				// left
				// if (keyLeft) {
					var back_ray = new THREE.Ray( back, l_vector );
					var back_intersects = back_ray.intersectScene( scene );
					if ( back_intersects.length > 0 && back_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.x = back_intersects[0].point.x+halfSize+1;
						console.warn("bl==="+back_intersects[0].object.name);
					}
					var front_ray = new THREE.Ray( front, l_vector );
					var front_intersects = front_ray.intersectScene( scene );
					if ( front_intersects.length > 0 && front_intersects[0].distance < halfSize ) {
						cubeRefMesh.position.x = front_intersects[0].point.x+halfSize+1;
						console.warn("fl==="+front_intersects[0].object.name);
					}
				// }
				cubeRefMesh.updateMatrix();
				collideMesh.position.y += 100;
			}

			function animate() {
				requestAnimationFrame( animate );
				loop();
			}

			function loop() {
				// control();

				camera.position.x = 0+cubeRefMesh.position.x-(cubeRefMesh.position.x/5);//200*Math.cos(r);
				camera.position.y = 200+cubeRefMesh.position.y;
				camera.position.z = cubeRefMesh.position.z+600;
				
				pointLight.position.x = 1000*Math.cos(r);
				pointLight.position.z = 1000*Math.sin(r);

				r += 0.01;
				
				if ( render_gl && has_gl ) webglRenderer.render( scene, camera );

				stats.update();

			}


		</script>

	</body>
</html>
